(function()
{
	var treeFun = function()
	{
		this.createTimeMs;
		this.functions = {};
		this.leftWidth = app.vm ? 150 : 210;
		this._setEvent();
		this.firstCon = true;
		this.curFuntion = null;
	};
	treeFun.prototype.show = function(backfun)
	{
		this.createTimeMs = new Date().getTime();
		this.functions = {};
		var minh = $(window).height() - app.getFullLayerPaddingY();
		var stLeft = 'width:' + this.leftWidth + 'px;min-height:' + minh + 'px;padding-right:16px';
		var stRight = 'margin-left:' + (this.leftWidth + 6) + 'px';
		var html = '<div class="maincontenttree" style="overflow-x:auto;position: relative;"><div style="min-width:1280px">';
		html += '<div class="left" style="' + stLeft + '"></div><div class="rightbottom right" style="' + stRight + '"></div>';
		html += '</div></div>';
		left.setMainContentHtml(html, null, true);
		var that = this;
		setTimeout(function()
		{
			if (backfun)
				backfun();
			if (that.curFuntion)
			{
				if (that.curFuntion.jqObj)
				{
					var self = $(that.curFuntion.jqObj);
					self.addClass('blue');
					self.removeClass('hui');
					that.oldSelf = self;
				}
				that.firstCon = true;
				that.curFuntion(that.curFuntion.data);
			}
		}, 333);
	}
	treeFun.prototype.caption = function(caption, pop, clear)
	{
		if (!this.captions)
			this.captions = [];
		if (arguments.length == 0)
		{
			var r = '';
			for (var i = 0; i < this.captions.length; i++)
			{
				if (this.captions[i])
					r += (r ? ' &gt; ' : '') + this.captions[i];
			}
			return r ? ('<p style="margin-left:12px" class="blue">' + r) : '';
		}
		else if (clear)
			this.captions = [];
		else if (pop)
			this.captions.pop();
		else
			this.captions.push(caption || '');
	}
	treeFun.prototype.addRightBottom = function(html, caption)
	{
		if (!this.firstCon)
		{
			this.ai = this.ai ? (this.ai + 1) : 1;
			var close = '<div class="fa fa-close red s16 pointer" onclick="$(this).parent().remove();tree.caption(0,true)" style="text-align:center;width:16px;height:16px;position:absolute;top:0;right:0"><i class=""></i></div>';
			var htmls = '<div class="pop" style="z-index:' + this.ai + ';top:8px;right:0;left:' + this.leftWidth + 'px;bottom:0;overflow:auto;position:absolute;background:#fff" >';
			if (caption)
			{
				this.caption(caption);
				htmls += this.caption();
			}
			htmls += html + close + '</div>';
			$('.maincontenttree').append(htmls);
			return;
		}
		this.firstCon = false;
		$('.maincontenttree>div.pop').remove();
		this.caption('', false, true);
		$('.maincontenttree .rightbottom').html(html);
	}
	treeFun.prototype.addLeft = function(path, caption, fun, loadDirFun, data, icon)
	{
		if (!path)
		{
			errorMsg('addLeft error : nopath');
			return;
		}
		var functions = this.functions;
		var boxlist = '.maincontenttree .left';
		var a = path.split('.');
		for (var i = 0; i < a.length - 1; i++)
		{
			var d = a[i];
			if (functions[d] === undefined)
			{
				errorMsg('addLeft error , no parent path : ' + path);
				return;
			}
			else if (typeof functions[d].fun == 'function')
			{
				errorMsg('addLeft error , parent path not dir : ' + path);
				return;
			}
			functions = functions[d];
			boxlist += ' div.' + d;
		}
		if (!this.curFuntion && fun)
		{
			this.curFuntion = fun;
			this.curFuntion.jqObj = boxlist + ' a.' + a[a.length - 1] + ' i.cmd';
		}
		// 尝试显示父窗口
		var fa = $(boxlist).prev('a').find('i');
		if (fa.hasClass('hui') && fa.hasClass('fa-plus-square-o'))
		{
			fa.removeClass('hui');
			fa.addClass('fa-minus-square-o');
			fa.removeClass('fa-plus-square-o');
			$(boxlist).show();
		}
		icon = icon || 'fa-circle';
		var h = '';
		var k = a[a.length - 1];
		var title = caption.replace(/\'\"/g, '').replace(/<\/?.+?>/g, "").trim();
		h += '<a id="' + k + '" ' + (title.length > 10 ? ('title="' + title + '"') : '') + ' class="a cmd pre ellipsis ' + k + '"><i class="fa ' + (fun ? ('fa hui cmd ' + icon) : 'fa-plus-square-o dir') + '"></i> ' + caption + '</a>';
		if (fun && data)
			fun.data = data;
		if (loadDirFun && data)
			loadDirFun.data = data;
		functions[k] = {
			fun : fun ? fun : {},
			loadDirFun : loadDirFun ? loadDirFun : {},
		};
		if (!fun)
		{
			h += '<div id="' + k + '" class="suba ' + k + '"></div>';
		}
		$(boxlist).append(h);
	}
	treeFun.prototype.showDir = function(boxlist, tid)
	{
		var self = $(boxlist + ' a.' + tid + ' i.dir');
		boxlist = $(boxlist + ' div.' + tid);
		if (boxlist.length <= 0 || !boxlist.html())
		{
			self.removeClass('fa-minus-square-o');
			self.addClass('fa-plus-square-o');
			self.addClass('hui');
		}
		else if (boxlist.is(':visible'))
		{
			boxlist.hide();
			boxlist.find('div').hide();
			boxlist.find('a i.dir').removeClass('fa-minus-square-o');
			boxlist.find('a i.dir').addClass('fa-plus-square-o');
			self.removeClass('fa-minus-square-o');
			self.addClass('fa-plus-square-o');
		}
		else
		{
			boxlist.show();
			self.addClass('fa-minus-square-o');
			self.removeClass('fa-plus-square-o');
		}
	}
	// 初始化对象
	treeFun.prototype._setEvent = function()
	{
		if (treeFun.prototype._)
			return;
		treeFun.prototype._ = 1;
		var that = this;
		$('body').on('click', '.maincontenttree .left a', function()
		{
			var a = [];
			var t = $(this);
			var tid = this.id;
			while (true)
			{
				if (t && t.parent())
				{
					t = t.parent();
					if (t.length > 0)
					{
						var id = t[0].id;
						if (id && t.hasClass('suba'))
						{
							a.push(id);
							continue;
						}
					}
				}
				break;
			}
			var fun = that.functions;
			var boxlist = '.maincontenttree .left';
			for (var i = a.length - 1; i >= 0; i--)
			{
				if (fun[a[i]])
				{
					fun = fun[a[i]];
					boxlist += ' div.' + a[i];
				}
			}
			if (fun[tid])
			{
				if (typeof fun[tid].fun == 'function')
				{
					var jqObj = boxlist + ' a.' + tid + ' i.cmd';
					var self = $(jqObj);
					if (that.oldSelf)
					{
						that.oldSelf.addClass('hui');
						that.oldSelf.removeClass('blue');
					}
					self.addClass('blue');
					self.removeClass('hui');
					that.oldSelf = self;
					that.firstCon = true;
					var curFuntion = fun[tid].fun;
					curFuntion.jqObj = jqObj;
					curFuntion(tid, curFuntion.data);
				}
				else
				{
					that.showDir(boxlist, tid);
					if (typeof fun[tid].loadDirFun == 'function')
					{
						fun[tid].loadDirFun();
						fun[tid].loadDirFun = null;
					}
				}
			}
		});
	}
	window.tree = new treeFun();
}());
